package uk.ac.cam.juliet.zedcat.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//An abstract superclass for the database interfaces
//For this, and any subclasses to work, you  need to have the JConnector in your classpath!
//In the SVN repos, it is in MySQLJConnector

public abstract class JulietDbInterface {
	protected Connection databaseConnection = null;
	private String databaseAddress = null;
	private String username = null;
	private String password = null;
	
	public JulietDbInterface(String address, String username, String password) throws ClassNotFoundException, SQLException
	{
		this.databaseAddress = address;
		this.username = username;
		this.password = password;
		
		Class.forName("com.mysql.jdbc.Driver");
		// databaseConnection = DriverManager.getConnection("jdbc:mysql:" + getDatabaseAddress(), getUsername(), password);
		// databaseConnection.setAutoCommit(false);
		
	}
	
	public void closeConnection() throws SQLException
	{
		if(databaseConnection != null)
		{
			databaseConnection.close();
		}
		databaseConnection = null;
	}
	
	public void connect() throws SQLException
	{
		this.closeConnection();
		databaseConnection = DriverManager.getConnection("jdbc:mysql:" + getDatabaseAddress(), username, password);
		databaseConnection.setAutoCommit(false);
	}
	
	public void setDatabaseAddress(String databaseAddress) {
		this.databaseAddress = databaseAddress;
	}

	public String getDatabaseAddress() {
		return databaseAddress;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsername() {
		return username;
	}
	
	public void setPassword(String password)
	{
		this.password = password;
	}
}
